package top.wilsonlv.jaguar.cloud.upms.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.wilsonlv.jaguar.cloud.upms.controller.vo.MenuVO;
import top.wilsonlv.jaguar.cloud.upms.controller.vo.ResourceServerVO;
import top.wilsonlv.jaguar.cloud.upms.sdk.enums.ClientType;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.UserVO;
import top.wilsonlv.jaguar.cloud.upms.service.PersonalService;
import top.wilsonlv.jaguar.cloud.upms.service.UserService;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;
import top.wilsonlv.jaguar.security.util.SecurityUtil;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author lvws
 * @since 2021/11/1
 */
@RestController
@RequestMapping("/personal")
@RequiredArgsConstructor
@Api(tags = "当前登录用户API")
public class PersonalApi {

    private final UserService userService;

    private final PersonalService personalService;

    @ApiOperation(value = "获取个人信息")
    @GetMapping("/info")
    public JsonResult<UserVO> info() {
        return JsonResult.success(userService.getDetail(SecurityUtil.getCurrentUserId(), true, false));
    }

    @ApiOperation(value = "修改个人信息")
    @PutMapping("/info")
    public JsonResult<UserVO> info(@RequestParam(required = false) String userPhone,
                                   @RequestParam(required = false) String userEmail,
                                   @RequestParam(required = false) String userNickName,
                                   @RequestBody(required = false) String userFaceImage) {
        Long currentUserId = SecurityUtil.getCurrentUserId();
        personalService.modifyUserInfo(currentUserId, userPhone, userEmail, userNickName, userFaceImage);
        return info();
    }

    @ApiOperation(value = "修改个人密码")
    @PutMapping("/password")
    public JsonResult<Void> password(@RequestParam @NotBlank String oldPassword,
                                     @RequestParam @NotBlank String newPassword) {
        personalService.modifyPassword(SecurityUtil.getCurrentUserId(), oldPassword, newPassword);
        return JsonResult.success();
    }

    @ApiOperation(value = "获取个人菜单")
    @GetMapping("/menus")
    public JsonResult<List<MenuVO>> menus(@RequestParam @NotBlank String serverId,
                                          @RequestParam(required = false) ClientType clientType) {
        return JsonResult.success(personalService.getUserMenus(SecurityUtil.getCurrentUserId(), serverId, clientType));
    }

    @ApiOperation(value = "获取个人资源服务")
    @GetMapping("/resourceServers")
    public JsonResult<List<ResourceServerVO>> resourceServers() {
        return JsonResult.success(personalService.getUserResourceServers(SecurityUtil.getCurrentUserId()));
    }

}
